<template>
  <!--     v-if 和 getters 都有监听的作用 当数值改变时 getters的值瞬时改变 if 监听     -->
  <div v-if="$store.getters['user/isLogin']">
    <div class="member">
      <!-- 写法 因为分模块来写 所以要访问 store 里的 getters 中 [模块名/ 方法名] -->
      <!-- 一个分页面 访问 Vuex里分模块的 getter数据 -->
      <div class="header-con">
        <!-- 这里 的:to="{ name: 'login' }"是一个命名式跳转 对应的要跳转的router下添加login的姓名 -->
          <router-link :to="{ name: 'login' }" class="mui-navigate-right"
          >    
          <div class="user-info">
            <div class="avatar-con">
                
              <div class="avatar">
                <img
                  src="../assets/images/avatar_default.png"
                  class="image-info"
                /> 
              </div>
            </div>
            <div class="person-con">
                <span>{{ $store.state.user.username }}</span>
            </div>
          </div>
        </router-link>
      </div>
    </div>
    <ul class="mui-table-view">
      <li class="mui-table-view-cell mui-media">
        <router-link :to="{ name: 'order_list' }" class="mui-navigate-right">
          <img
            class="mui-media-object mui-pull-left"
            src="../assets/images/avatar_default.png"
          />
          <div class="mui-media-body">我的订单</div>
        </router-link>
      </li>
      <li class="mui-table-view-cell mui-media">
        <router-link to="/user/address" class="mui-navigate-right">
          <img
            class="mui-media-object mui-pull-left"
            src="../assets/images/avatar_default.png"
          />
          <div class="mui-media-body">收货地址</div>
        </router-link>
      </li>
      <li class="mui-table-view-cell mui-media">
        <div class="mui-navigate-right">
          <img
            class="mui-media-object mui-pull-left"
            src="../assets/images/avatar_default.png"
          />
          <div class="mui-media-body" @click="logout">退出登录</div>
        </div>
      </li>
    </ul>
  </div>
  <div class="member" v-else>
        
    <div class="header-con">
      <!-- 这里 的:to="{ name: 'login' }"是一个命名式跳转 对应的要跳转的router下添加login的姓名 -->
        <router-link :to="{ name: 'login' }" class="mui-navigate-right"
        >    
        <div class="user-info">
          <div class="avatar-con">
              
            <div class="avatar">
              <img
                src="../assets/images/avatar_default.png"
                class="image-info"
              />
                
            </div>
          </div>
          <div class="person-con">  <span>登录 / 注册</span></div>
                  
        </div>
      </router-link>

          
    </div>
      
  </div>
</template>

<script>
export default {
  name: "user",
  methods: {
    logout() {
      // 发送请求 完成退出登录
      this.$http.get("logout");
      // 将localStroge里面的Authentic干掉
      this.$auth.setAuthorization("");
      // 3.将vuex中的用户信息删除
      this.$store.commit("user/logout");
      // 4.提示用户退出
      this.$toast("退出登录成功");
    },
  },
};
</script>

<style   >
.mui-media-body {
  line-height: 42px;
  text-align: left;
}
.mui-table-view-cell:after {
  left: 0px;
}
.member {
  margin-bottom: 15px;
}
.member .header-con {
  padding: 10px;
  background-color: #fff;
}
.member .user-info {
  position: relative;
  overflow: hidden;
  height: 120px;
  background: linear-gradient(90deg, #28a2ff, #ffd787);
  box-shadow: 0 0.1rem 0.25rem #f8e3c6;
  width: 100%;
}
.avatar-con {
  position: absolute;
  left: 15px;
  top: 50%;
  transform: translateY(-50%);
}
.avatar {
  width: 60px;
  height: 60px;
  overflow: hidden;
  box-shadow: 0 2px 10px rgba (0, 0, 0, 15);
  border: 1px solid hsla (0, 0%, 100%, 0.4);
  border-radius: 50%, 50%;
}
.image-info {
  width: 100%;
  height: 100%;
}
.person-con {
  position: absolute;
  left: 90px;
  top: 50%;
  transform: translateY(-50%);
  color: #fff;
}
</style>
 